<template>
  <div>
    <basic-container>
      <div style="margin: 10px 0 10px 25px">
        <el-switch
          v-model="statusValue"
          @change="handleStatusChange"
          active-color="#13ce66"
          active-text="正常营业"
          inactive-text	="紧急关店"
          inactive-color="#ff4949">
        </el-switch>
        <div style="font-size: 8px; margin-top: 8px; color: lightgray">备注：关闭后用户将不可下单</div>
      </div>
      <avue-form :option="option"
                 v-model="form"
                 @tab-click="handleTabClick"
                 @submit="handleSubmit"></avue-form>
    </basic-container>
  </div>
</template>

<script>
import option from "@/const/merchant/info";
import {getInfo, statusChange, update} from "@/api/merchant/merchant";
import func from "@/util/func";


export default {
  data() {
    return {
      index: 0,
      option: option,
      form: {},
      statusValue: true
    };
  },
  created() {
    this.handleWitch();
  },
  methods: {
    handleSubmit(form, done) {
      if (this.index === 0) {
        if (form.longitudeAndLatitude) {
          form.longitudeAndLatitude = JSON.stringify(form.longitudeAndLatitude);
        }
        update(form).then(res => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "修改信息成功!"
            });
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            });
          }
          done();
        }, error => {
          window.console.log(error);
          done();
        })
      } else {
        done()
        // updatePassword(form.oldPassword, form.newPassword, form.newPassword1).then(res => {
        //   if (res.data.success) {
        //     this.$message({
        //       type: "success",
        //       message: "修改密码成功!"
        //     });
        //   } else {
        //     this.$message({
        //       type: "error",
        //       message: res.data.msg
        //     });
        //   }
        //   done();
        // }, error => {
        //   window.console.log(error);
        //   done();
        // })
      }
    },
    handleWitch() {
      if (this.index === 0) {
        getInfo().then(res => {
          this.form = res.data.data
          this.statusValue = this.form.status === 1
          console.log(this.statusValue)
          if (this.form.longitudeAndLatitude) {
            this.form.longitudeAndLatitude = JSON.parse(this.form.longitudeAndLatitude)
          }
        });
      }
    },
    handleTabClick(tabs) {
      this.index = func.toInt(tabs.index);
      this.handleWitch();
    },
    handleStatusChange() {
      statusChange(this.form.id).then(() => {
        this.handleWitch()
      })
    }
  }
};
</script>

<style>
</style>
